﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Ace;
using Ace.Application.Common;
using Ace.Application.Wiki;
using Ace.Entity.Wiki;
using Ace.IdStrategy;
using Ace.Web.Mvc;
using Ace.Web.Mvc.Authorization;
using Ace.Web.Mvc.Models;
using Chloe.Admin.Common;
using Chloe.Admin.Common.Tree;
using Microsoft.AspNetCore.Mvc;
using OfficeOpenXml;

namespace Chloe.Admin.Areas.Wiki.Controllers
{

    [Area(AreaNames.Wiki)]
    [Permission("wiki.shoporder")]
    public class ShopOrderController : WebController<IShopOrderService>
    {
        public IActionResult Index()
        {
            IShopService SortService = this.CreateService<IShopService>();
            List<SimpleShop2> ListShop = SortService.GetCacheList2();
            ViewBag.ListShop = ListShop;

            //IUsersService UserService = this.CreateService<IUsersService>();
            //List<Users> ListUser = UserService.GetList();
            //this.ViewBag.Users = ListUser.Select(a => new Select2Item() { id = a.Id, text = a.UserName });

            return View();
        }

        [HttpGet]
        public ActionResult GetModels(Pagination pagination, int ShopID = 0,int ST=-1, string time1 = "", string time2 = "", string OrderCode = "", int TP = -1)
        {
            PagedData<ShopOrderInfo> pagedData = this.Service.GetPageOrderList(pagination, ShopID,"",ST, time1, time2, OrderCode, TP);
            return this.SuccessData(pagedData);
        }


        [Permission("wiki.shoporder.delete")]
        [HttpPost]
        public ActionResult Delete(string id)
        { 
            this.Service.Delete(id);
            return this.SuccessMsg("删除成功");
        }

        


        [Permission("wiki.shoporder.edit")]
        [HttpPost]
        public ActionResult Update(UpdateShopOrderInput modle)
        {
            this.Service.Update(modle);
            return this.UpdateSuccessMsg();
        }

        [Permission("wiki.shoporder.add")]
        [HttpPost]
        public ActionResult Add(AddShopOrderInput modle)
        {
            modle.CreateDate = DateTime.Now;

            this.Service.Add(modle);
            return this.AddSuccessMsg();
        }


        public ActionResult Item(string Id)
        {
            ViewBag.Id = Id;
            IShopOrderService orderService = this.CreateService<IShopOrderService>();
            ShopOrder order = orderService.GetModel(Id);
            ViewBag.order = order;
            ViewBag.OrderStatuName = orderService.GetST_Name(order.ST);

            Users users = this.CreateService<IUsersService>().GetModel(order.CreateID);
            ViewBag.users = users;

            if (order.AddressID != "0" && order.AddressID != "1")
            {
                Users_Address address = this.CreateService<IUsers_AddressService>().GetModel(order.AddressID);
                ViewBag.address = address;
            }
            else
            {
                Users_Address address = new Users_Address{ Address = "", GPS_X = 0, GPS_Y = 0, Id = "", CreateID = "", IsDefault = 0, Mobile="", UserName="" };
                ViewBag.address = address;
            }
            

            Shop shop = this.CreateService<IShopService>().GetModel(order.ShopID);
            ViewBag.shop = shop;

            

            return View();
        }

        [HttpGet]
        public ActionResult GetItemModels(Pagination pagination,string Id)
        {
            IShopOrderItemService shopOrderItemService = this.CreateService<IShopOrderItemService>();
            //PagedData<ShopOrderItem> pagedData = shopOrderItemService.GetPageData(pagination, Id);
            List<ShopOrderItemInfo> list = shopOrderItemService.GetOrderItemList(Id);
            PagedData<ShopOrderItemInfo> pagedData = new PagedData<ShopOrderItemInfo>();
            pagedData.CurrentPage = pagination.Page;
            pagedData.Models = list;
            pagedData.PageSize = pagination.PageSize;
            pagedData.TotalCount = list.Count;
            


            return this.SuccessData(pagedData);
        }


        [Permission("wiki.shoporder.delete")]
        [HttpPost]
        public ActionResult DeleteItem(string id)
        {
            IShopOrderItemService shopOrderItemService = this.CreateService<IShopOrderItemService>();
            shopOrderItemService.Delete(id);
            return this.SuccessMsg("删除成功");
        }

        [HttpPost]
        public ActionResult UpdateItem(UpdateShopOrderInput modle)
        {
            this.Service.Update(modle);
            return this.UpdateSuccessMsg();
        }




        [HttpGet]
        public ActionResult Export(Pagination pagination, int ShopID = 0, int ST = -1, string time1 = "", string time2 = "", string OrderCode = "")
        {
            //string sWebRootFolder = _hostingEnvironment.WebRootPath;
            string physicalFilePath = Directory.GetCurrentDirectory() + "\\" + Globals.Configuration["AppSettings:FileRootDir"] + "\\" + Globals.Configuration["AppSettings:UploadXls"];

            string sFileName = @"订单" + DateTime.Now.ToString("yyyy-MM-dd") + ".xlsx";
            string webUrl = "/" + Globals.Configuration["AppSettings:UploadXls"] + "/" + sFileName;
            //string URL = string.Format("{0}://{1}/{2}", Request.Scheme, Request.Host, sFileName);
            string FilePath = Path.Combine(physicalFilePath, sFileName);
            FileInfo file = new FileInfo(FilePath);
            if (file.Exists)
            {
                file.Delete();
                file = new FileInfo(FilePath);
            }

             

            PagedData<ShopOrderInfo> pagedData = this.Service.GetPageOrderList(pagination, ShopID, "", ST, time1, time2, OrderCode);
            


            using (ExcelPackage package = new ExcelPackage(file))
            {
                // add a new worksheet to the empty workbook
                ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Employee");
                //First add the headers
                worksheet.Cells[1, 1].Value = "订单号";
                worksheet.Cells[1, 2].Value = "订单金额";
                worksheet.Cells[1, 3].Value = "商品金额";
                worksheet.Cells[1, 4].Value = "客户名称";
                worksheet.Cells[1, 5].Value = "手机";
                worksheet.Cells[1, 6].Value = "收货地址";

                worksheet.Cells[1, 7].Value = "下单时间";
                worksheet.Cells[1, 8].Value = "快递公司";
                worksheet.Cells[1, 9].Value = "快递单号";
                worksheet.Cells[1, 10].Value = "交易订单号";

                int m = 0;
                //Add values
                for (var i = 0; i < pagedData.Models.Count; i++)
                {
                    m = i + 2;
                    worksheet.Cells["A" + m].Value = pagedData.Models[i].OrderCode;
                    worksheet.Cells["B" + m].Value = pagedData.Models[i].Total.ToString();
                    worksheet.Cells["C" + m].Value = pagedData.Models[i].ProTotal.ToString();
                    worksheet.Cells["D" + m].Value = pagedData.Models[i].UserName.ToString();
                    worksheet.Cells["E" + m].Value = pagedData.Models[i].Mobile.ToString();
                    worksheet.Cells["F" + m].Value = pagedData.Models[i].Address;
                    worksheet.Cells["G" + m].Value = pagedData.Models[i].CreateDate.ToString("yyyy-MM-dd HH:mm");
                    worksheet.Cells["H" + m].Value = pagedData.Models[i].express_name;
                    worksheet.Cells["I" + m].Value = pagedData.Models[i].express_code;
                    worksheet.Cells["J" + m].Value = pagedData.Models[i].transaction_id;
                }


                package.Save(); //Save the workbook.
            }
            //return File(new FileStream(FilePath, FileMode.Open), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "订单.xlsx");
            return this.SuccessData(webUrl);
        }



    }
}